/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.to.aboomy.bannersample.pagetransformer;

import com.to.aboomy.banner.BannerViewPager;
import ohos.agp.components.Component;

/**
 * DepthPageTransformer
 *
 * @since 2021-08-05
 */
public class DepthPageTransformer implements BannerViewPager.PageTransformer {
    private static final float NUM_F_0_5 = 0.5f;
    private static final float NUM_F_0_75 = 0.75f;
    private static final float NUM_F_0_25 = 0.25f;

    @Override
    public void transformerPage(BannerViewPager.PageType pageType, Component page, float offset, float offsetPixels) {
        switch (pageType) {
            case PREVIOUS:
                page.setTranslationX(0.0F);
                page.setScaleX(1.0F);
                page.setScaleY(1.0F);
                page.setAlpha(1f);
                break;
            case CURRENT:
                if (offset <= 0.0F) {
                    page.setTranslationX(0.0F);
                    page.setScaleX(1.0F);
                    page.setScaleY(1.0F);
                    page.setAlpha(1f);
                } else if (offset <= 1.0F) {
                    float scaleFactor = NUM_F_0_75 + NUM_F_0_25 * ((float) 1 - Math.abs(offset));
                    page.setAlpha((float) 1 - offset);
                    page.setPivotY(NUM_F_0_5 * (float) page.getHeight());
                    page.setTranslationX((float) page.getWidth() * -offset);
                    page.setScaleX(scaleFactor);
                    page.setScaleY(scaleFactor);
                }
                break;

            case NEXT:
                float nextFloat = 1 - offset;
                if (nextFloat <= 0.0F) {
                    page.setTranslationX(0.0F);
                    page.setScaleX(1.0F);
                    page.setScaleY(1.0F);
                    page.setAlpha(1f);
                } else if (offsetPixels > 0) {
                    float scaleFactor = NUM_F_0_75 + NUM_F_0_25 * ((float) 1 - Math.abs(nextFloat));
                    page.setAlpha((float) 1 - nextFloat);
                    page.setPivotY(NUM_F_0_5 * (float) page.getHeight());
                    page.setTranslationX((float) page.getWidth() * -nextFloat);
                    page.setScaleX(scaleFactor);
                    page.setScaleY(scaleFactor);
                }
                break;

            case OTHER:
                page.setTranslationX(0.0F);
                page.setScaleX(1.0F);
                page.setScaleY(1.0F);
                page.setAlpha(1f);
                break;
            default:
                break;
        }
    }
}
